<template>
  <div class="home">
    <mbx name1="商品管理" name2="参数列表" />
    <!-- card -->
    <el-card class="mt16">
      <el-alert
        title="注意：只允许为第三级分类设置相关参数！"
        type="warning"
        show-icon
        effect="dark"
      >
      </el-alert>
      <!-- 选择商品分类 -->
      <el-row class="mt16">
        <el-col :span="3">选择商品分类：</el-col>
        <el-col :span="8"
          ><el-cascader
            v-model="catarr"
            :options="options"
            :props="{
              expandTrigger: 'hover',
              value: 'cat_id',
              label: 'cat_name',
              children: 'children'
            }"
            @change="getParamsData()"
          ></el-cascader
        ></el-col>
      </el-row>
      <!-- 卡片式 -->
      <el-tabs
        v-model="activeName"
        type="border-card"
        class="mt16"
        @tab-click="handleClick"
      >
        <el-tab-pane label="动态参数" name="many">
          <!-- 添加按钮部分 -->
          <el-row>
            <el-col :span="8">
              <el-button
                type="primary"
                :disabled="cd"
                @click="dialogFormVisible = true"
                >添加参数</el-button
              >
            </el-col>
          </el-row>
          <!-- 表格区 -->
          <el-table :data="dong" border style="width: 100%" class="mt16">
            <el-table-column fixed width="50" type="expand">
              <template slot-scope="scope">
                <el-tag
                  :key="index"
                  v-for="(item, index) in scope.row.attr_vals"
                  closable
                  :disable-transitions="false"
                  @close="handleClose(scope.row, index)"
                >
                  {{ item }}
                </el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(scope.row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column type="index" label="#"> </el-table-column>
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <el-table-column fixed="right" label="操作">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  size="mini"
                  class="el-icon-edit"
                  @click="edit(scope.row)"
                  >编辑</el-button
                >
                <el-button
                  type="danger"
                  size="mini"
                  class="el-icon-delete"
                  @click="del(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态参数" name="only">
          <!-- 添加按钮部分 -->
          <el-row>
            <el-col :span="8">
              <el-button
                type="primary"
                :disabled="cd"
                @click="dialogFormVisible = true"
                >添加参数</el-button
              >
            </el-col>
          </el-row>
          <!-- 表格区 -->
          <el-table :data="jing" border style="width: 100%" class="mt16">
            <el-table-column fixed width="50" type="expand">
              <template slot-scope="scope">
                <el-tag
                  :key="index"
                  v-for="(item, index) in scope.row.attr_vals"
                  closable
                  :disable-transitions="false"
                  @close="handleClose(scope.row, index)"
                >
                  {{ item }}
                </el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(scope.row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column type="index" label="#"> </el-table-column>
            <el-table-column prop="attr_name" label="参数名称">
            </el-table-column>
            <el-table-column fixed="right" label="操作">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  size="mini"
                  class="el-icon-edit"
                  @click="edit(scope.row)"
                  >编辑</el-button
                >
                <el-button
                  type="danger"
                  size="mini"
                  class="el-icon-delete"
                  @click="del(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
      <!-- 添加的模态框 -->
      <el-dialog
        title="添加动态参数"
        :visible.sync="dialogFormVisible"
        @close="qk"
      >
        <el-form :model="form" :rules="rules" ref="add" class="demo-ruleForm">
          <el-form-item label="添加动态参数" prop="attr_name">
            <el-input v-model="form.attr_name" style="width: 350px"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="add">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 编辑 -->
      <el-dialog
        title="修改动态参数"
        :visible.sync="dialogFormVisible1"
        @close="qk"
      >
        <el-form
          :model="form1"
          :rules="rules"
          ref="editadd"
          class="demo-ruleForm"
        >
          <el-form-item label="添加动态参数" prop="attr_name">
            <el-input v-model="form1.attr_name" style="width: 350px"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible1 = false">取 消</el-button>
          <el-button type="primary" @click="xg">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeName: 'many',
      options: [],
      form: {
        attr_name: '',
        attr_sel: [],
        id: 0
      },
      form1: {
        attr_name: '',
        attr_sel: [],
        id: 0
      },
      catarr: [],
      dong: [],
      jing: [],
      inputVisible: false, //控制input框显示
      inputValue: '',
      dialogFormVisible: false,
      dialogFormVisible1: false,
      rules: {
        attr_name: [{ required: true, message: '请输入内容', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.getlist()
  },
  methods: {
    // 渲染数据
    async getlist() {
      const res = await this.$API.getcate()
      console.log('分类参数数据', res)
      this.options = res
    },
    // 渲染动态或者静态数据
    async getParamsData() {
      if (this.catarr.length !== 3) {
        this.$message.error('必须选择三级的才可以呦!')
        this.dong = []
        this.jing = []
        return false
      }
      // 根据所选分类的id,和在当前所处的面板,获取数据
      const res = await this.$API.getcanshulist(this.catid, this.activeName)
      res.forEach((item) => {
        item.inputVisible = false
        item.inputValue = ''
        item.attr_vals =
          item.attr_vals.length > 0 ? item.attr_vals.split(',') : []
      })
      console.log('参数列表1', res)
      if (this.activeName == 'many') {
        this.dong = res
      } else if (this.activeName == 'only') {
        this.jing = res
      }
      console.log('动态', this.dong)
      console.log('静态', this.jing)
    },
    // tabs栏切换时
    handleClick() {
      this.getParamsData()
    },
    // 失去焦点时和点击变为input框
    async handleInputConfirm(row) {
      if (row.inputValue.trim().length > 0) {
        row.attr_vals.push(row.inputValue)
        await this.$API.addtag(this.catid, row.attr_id, {
          attr_name: row.attr_name,
          attr_sel: this.activeName,
          attr_vals: row.attr_vals.join(',')
        })
      }
      row.inputVisible = false
      row.inputValue = ''
    },
    // input框打开
    showInput(row) {
      row.inputVisible = true
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    // 删除当前行参数
    del(row) {
      this.$confirm('此操作将永久删除该参数, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await this.$API.deltag(this.catid, row.attr_id)
          this.getParamsData()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 删除某一行参数
    async handleClose(row, index) {
      this.$confirm('此操作将永久删除该参数, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          row.attr_vals.splice(index, 1)
          await this.$API.addtag(this.catid, row.attr_id, {
            attr_name: row.attr_name,
            attr_sel: this.activeName,
            attr_vals: row.attr_vals.join(',')
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 添加动态参数
    add() {
      this.form.id = this.catid
      this.form.attr_sel = this.activeName
      this.$refs['add'].validate(async (valid) => {
        if (valid) {
          const res = await this.$API.adddjlist(this.form)
          this.form.attr_name = ''
          this.dialogFormVisible = false
          this.getParamsData()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 清空
    qk() {
      this.form.attr_name = ''
    },
    // 动态编辑
    edit(row) {
      this.dialogFormVisible1 = true
      this.form1.attr_name = row.attr_name
      this.form1.id = this.catid
      this.form1.attr_sel = this.activeName
      this.form1.attr_id = row.attr_id
      this.form1.attr_vals = row.attr_vals.join(',')
    },
    xg() {
      this.$refs['editadd'].validate(async (valid) => {
        if (valid) {
          const res = await this.$API.addtag(this.catid, this.form1.attr_id, {
            attr_name: this.form1.attr_name,
            attr_sel: this.activeName,
            attr_vals: this.form1.attr_vals
          })
          this.getParamsData()
          this.dialogFormVisible1 = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  computed: {
    // 判断按钮是否被禁用
    cd() {
      return this.catarr.length !== 3
    },
    // 三级分类里最后的id
    catid() {
      return this.catarr[this.catarr.length - 1]
    }
  }
}
</script>

<style lang="scss" scoped>
.home {
  width: 100%;
  .h {
    width: 100%;
    display: flex;
  }
  .el-tag {
    margin: 6px 10px;
  }
  .el-input {
    width: 100px;
  }
}
</style>
